#!/usr/bin/python

_Copyright = "Copyright @ 2010 Michael P. Reilly. All rights reserved."

_SCM_ID = "$Id: xmlhttp.py 81 2010-12-14 02:41:34Z arcege@gmail.com $"

from relinfo import ReleaseInfo
from kcapp.app import logging, main
from kcapp.database import *
from kcapp.reqhandler import XmlHttpRequestHandler

class XmlBaseDocument(object):
    def __init__(self, status, msg):
        self.status = status
        self.msg = msg
    def __str__(self):
        return '''\
<?xml version="1.0" encoding="ISO-8859-1"?>
<app>
<status>%s</status>
%s
</app>
''' % (self.status, self.msg)
class XmlDocument(XmlBaseDocument):
    def __init__(self, msg):
        super(XmlDocument, self).__init__(True, msg)
class XmlErrorDocument(XmlBaseDocument):
    def __init__(self, msg):
        super(XmlErrorDocument, self).__init__(False, '<error>%s</error>' % msg)

class NewSubmitXml(XmlHttpRequestHandler):
    def process_request(self):
        rname = str(self.request.get('name'))
        rtype = str(self.request.get('type'))
        rurl  = str(self.request.get('url'))
        runame= str(self.request.get('uname'))
        rpswd1 = str(self.request.get('pswd1'))
        rpswd2 = str(self.request.get('pswd2'))
        if rname == "":
            self.xmlhttpoutput = XmlErrorDocument('name is blank')
            return
        if rpswd1 != rpswd2:
            self.xmlhttpoutput = XmlErrorDocument('passwords do not match')
            return
        q = OSDBSeed.all()
        q.filter('author =', self.user)
        q.filter('name =', rname)
        if q.count > 0:
            self.xmlhttpoutput = XmlErrorDocument('Entry already exists')
            return
        vars = {
            'name': rname,
            'type': rtype,
            'url': rurl,
            'uname': runame,
            'pswd': rpswd1,
        }
        self.xmlhttpoutput = XmlDocument(True, '''\
<proceed>
  <name>%(name)s</name>
  <type>%(type)s</type>
  <url>%(url)s</url>
  <uname>%(uname)s</uname>
  <pswd>%(pswd)s</pswd>
</proceed>
''' % vars
        )

class NewConfirmXml(XmlHttpRequestHandler):
    def process_request(self):
        rname = str(self.request.get('name'))
        rtype = str(self.request.get('type'))
        rurl  = str(self.request.get('url'))
        runame= str(self.request.get('uname'))
        rpswd = str(self.request.get('pswd'))
        if rpswd:
            try:
                pswd = OSDBPassword(author=self.user, pswd=rpswd)
            except:
                self.xmlhttpoutput = XmlErrorDocument('password is required')
                return
            else:
                pswd.put()
                logging.info('create new password entry: key = %s' % pswd.key())
        else:
            pswd = None
        record = OSDBSeed(
            author=self.user, name=rname, type=rtype,
            labels=[], url=rurl, username=runame,
            password=pswd
        )
        record.put()
        logging.info('created new seed entry: key = %s' % record.key())
        self.xmlhttpoutput = XmlDocument(True, '''\
<message>Entry created</message>
<redirect>/main</redirect>
'''
        )

class LabelAssignXml(XmlHttpRequestHandler):
    pass

class LabelRemoveXml(XmlHttpRequestHandler):
    pass

URL_Map = [
    ('/rpc/xml/new/submit', NewSubmitXml),
    ('/rpc/xml/new/confirm', NewConfirmXml),
    ('/rpc/xml/label/assign', LabelAssignXml),
    ('/rpc/xml/label/remove', LabelRemoveXml),
]

if __name__ == '__main__':
    main(URL_Map)

